function print_r(theObj) {
    var retStr = '';
    if (typeof theObj == 'object') {
        retStr += '<div style="font-family:Tahoma; font-size:7pt;">';
        for (var p in theObj) {
            if (typeof theObj[p] == 'object') {
                retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
                retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>';
            } else {
                retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
            }
        }
        retStr += '</div>';
    }
    return retStr;
}

var dialogMessage = function (message) {
    $( "#dialog-tip" ).dialog( "destroy" );
    if(!$( "#dialog-tip" ).length){  
        $("body").append("<div id='dialog-tip' />");  
    } 
    $( "#dialog-tip" ).html('' + message + '');
    $( "#dialog-tip" ).dialog({
        bgiframe: true,
        title: $.languages['systemNotice'],  
        modal: true,
        buttons: [
            {
                text: $.languages['ok'],
                click: function() { $(this).dialog("close"); }
            },
        ]
    });
};

var dialogTip = function (message, callback) { 
    $( "#dialog-tip" ).dialog( "destroy" );
    if(!$( "#dialog-tip" ).length){  
        $("body").append("<div id='dialog-tip' />");  
    } 
    $( "#dialog-tip" ).html('' + message + '');
    $( "#dialog-tip" ).dialog({
        bgiframe: true,
        title: $.languages['systemNotice'],  
        modal: true
    });
    
    setTimeout(function(){
        $( "#dialog-tip" ).dialog( "destroy" );
        callback();
    }, 1200);
}

var dialogTipRedirect = function (message, url) { 
    var message = '<div class="messages">' + message + '</div><div class="redirecting"><a href="' + url + '">' + $.languages['redirecting'] + '</a></div>';
    
    dialogTip(message, function(){
        location.href = url;
    });
}

var dialogConfirm = function (message, callback)  { 
    $( "#dialog-confirm" ).dialog( "destroy" );
    if(!$( "#dialog-confirm" ).length){  
        $("body").append("<div id='dialog-confirm' />");  
    } 
    $( "#dialog-confirm" ).html(message);
    $( "#dialog-confirm" ).dialog({
        bgiframe: true,
        title: $.languages['systemNotice'],  
        modal: true,
        buttons: [
            {
                text: $.languages['ok'],
                click: function() { $(this).dialog("close"); callback();}
            },
            {
                text: $.languages['cancel'],
                click: function() { $(this).dialog("close"); }
            },
        ]
    });
}

jQuery.fn.reloadCaptcha=function () {
    var $captcah = this;
    $.ajax({
        type: "post",   
        url: $.url.captcha, 
        data: {},
        beforeSend: function() {   
        },   
        success:function(responseText, statusText){
            var json = jQuery.parseJSON(responseText);
            if(json.result != 1){
                dialogMessage(json.messages);
                return "";
            }

            $captcah.find("img").attr('src', json.data.url);
            $captcah.find("input[name='captcha\[id\]']").val(json.data.id);
            return true;
        }, 
    });
}

$(document).ready(function(){
    var loader = $('<div id="ajaxLoader"></div>');
    loader.ajaxStart(function() {
        $( "#ajaxLoaderDialog" ).dialog( "destroy" );
        if(!$( "#ajaxLoaderDialog" ).length){  
            $("body").append("<div id='ajaxLoaderDialog' />");  
        }
        $( "#ajaxLoaderDialog" ).html('<img src="' + $.url.base + 'ui/style/base/default/images/loading.gif" alt="loading..." />');
        $( "#ajaxLoaderDialog" ).dialog({
            bgiframe: true,
            title: 'Loading......',  
            modal: false,
            width: 320,
            height: 60,
            position: ['right', 'bottom'],
        });
    }).ajaxStop(function() {
        $( "#ajaxLoaderDialog" ).dialog( "destroy" );
    }).ajaxError(function(a, b, e) {
        throw e;
    });
    
    
    $('.buddy a').livequery("click", function(){
        var currIndex       = $('.buddy a').index($(this));
        var doudangBuddy    = $($('.buddy').get(currIndex));
        var doudangBuddyBox = $($('.buddy-box').get(currIndex));

        var buddyUserId = doudangBuddy.find("input[name='buddyUserId']").val();
        var buddy       = doudangBuddyBox.find("input[name='buddy']").val();

        $.ajax({
            type: "post",
            url: $.url.saveBuddy,
            data: {buddyUserId : buddyUserId, buddy : buddy},
            beforeSend: function(XMLHttpRequest){
                //ShowLoading();
            },
            success: function(responseText, statusText){
                //alert(responseText);
                var json = jQuery.parseJSON(responseText);
                if(json.result != 1){
                    dialogMessage(json.messages);
                    return false;
                }

                doudangBuddyBox.html(json.content)
                return true;
            },
            complete: function(responseText, statusText){
                //HideLoading();
            },
            error: function(){
            }
        });
    });
    
    
    $(".attachment-video").find(".photo").livequery("click", function(){
        var currIndex = $(".attachment-video").find(".photo").index($(this));
        var video = $($(".attachment-video").get(currIndex));

        var photo = video.find("img");
        var flash = video.find(".flash");

        photo.hide();
        flash.show();
                
        var url = video.find("input[name='flash']").val();

        var player = flash.find(".player");
        if(!player.html())
        {
            player.flash({
                src: url,
                width: '100%',
                height: 500,
                allowFullScreen:'true',
            },
            {
                version: 8
            });
        }
    });
    $(".attachment-video").find(".close").livequery("click", function(){
        var currIndex = $(".attachment-video").find(".close").index($(this));
        var video = $($(".attachment-video").get(currIndex));
        
        var photo = video.find("img");
        var flash = video.find(".flash");

        photo.show();
        flash.hide();
    });
    
    $(".attachment-image").find(".photo").livequery("click", function(){
        var currIndex = $(".attachment-image").find(".photo").index($(this));
        var image = $($(".attachment-image").get(currIndex));
        
        image.find(".photo").hide();
        image.find(".big-image").show();
    });
    $(".attachment-image").find(".big-image").livequery("click", function(){
        var currIndex = $(".attachment-image").find(".big-image").index($(this));
        var image = $($(".attachment-image").get(currIndex));
        
        image.find(".big-image").hide();
        image.find(".photo").show();
    });
    
    $(".attachment-shopping").find(".photo").livequery("click", function(){
        var currIndex = $(".attachment-shopping").find(".photo").index($(this));
        var image = $($(".attachment-shopping").get(currIndex));
        
        image.find(".photo").hide();
        image.find(".big-image").show();
    });
    $(".attachment-shopping").find(".big-image").livequery("click", function(){
        var currIndex = $(".attachment-shopping").find(".big-image").index($(this));
        var image = $($(".attachment-shopping").get(currIndex));
        
        image.find(".big-image").hide();
        image.find(".photo").show();
    });
    
        
    $(".captcha").find("img").livequery("click", function(){
        var currIndex = $(".captcha").find("img").index($(this));
        var captcha = $($('.captcha').get(currIndex));
        captcha.reloadCaptcha();
    });
    
    $(".article-access").find(".delete").live('click',function(){ 
        var $articleId   = $(this).attr("data-deleteArticleId");
        var $currArticle = $("div[data-articleId=" + $articleId + "]");

        var $ok     = $.languages.ok;
        var $cancel = $.languages.cancel;

        dialogConfirm($.languages.areYouSureToDelete, function(){
            $.ajax({ 
                type: "GET", 
                url: $.url.deleteArticle, 
                data: { articleId : $articleId},
                success:function(responseText, statusText){
                    var json = jQuery.parseJSON(responseText);
                    if(json.result != 1){
                        dialogMessage(json.messages);
                        return false;
                    }

                    $currArticle.html('<span class="ui-icon ui-icon-notice column"></span>' + $.languages['blogIsDelete']); 
                    return true;
                } 
            });
        });

    }); 

});